﻿Imports System.Data.SqlClient
Imports System.Data

Partial Class callinfo3
    Inherits System.Web.UI.Page
    Dim conn As New SqlConnection("Data Source=sql.bluetonemedia.com;Initial Catalog=BlueToneMediaDB;Persist Security Info=True;User ID=BTMWiz;Password=esquel4ma@gic")
    'Dim conn2 As New SqlConnection("Data Source=sql.bluetonemedia.com;Initial Catalog=BlueToneMediaDB;Persist Security Info=True;User ID=BTMWiz;Password=esquel4ma@gic")
    Dim Qry As String
    Dim Qry2 As String
    Dim Qry3 As String
    Dim Qry4 As String
    Dim dv As New DataView
    Dim dv2 As New DataView

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Dim phone As String

        'phone = "+19103868470"

        phone = Request.QueryString("number")

        Dim area, pre, ends As String

        area = ""
        pre = ""
        ends = ""

        area = Mid(phone, 3, 3)
        pre = Mid(phone, 6, 3)
        ends = Right(phone, 4)

        Qry = "SELECT * FROM  Companys WHERE Phone LIKE '%" & area & "%' AND Phone LIKE '%" & pre & "%' AND Phone LIKE '%" & ends & "%' ORDER BY id DESC"
        conn.Open()
        Dim Sample As New DataSet
        Dim Sql As New SqlDataAdapter(Qry, conn)
        Sql.Fill(Sample)
        dv.Table = Sample.Tables(0)

        Qry2 = "SELECT * FROM  Contacts WHERE Mobile LIKE '%" & area & "%' AND Mobile LIKE '%" & pre & "%' AND Mobile LIKE '%" & ends & "%' ORDER BY id DESC"
        Dim Sample2 As New DataSet
        Dim Sql2 As New SqlDataAdapter(Qry2, conn)
        Sql2.Fill(Sample2)
        dv2.Table = Sample2.Tables(0)

        Dim myid As Integer
        Dim exist As Boolean
        Dim which As Integer
        Dim which2 As Integer


        which = Sample.Tables(0).Rows().Count
        which2 = Sample2.Tables(0).Rows().Count

        If which = 0 And which2 = 0 Then
            exist = False
        Else
            If which = 0 Then
                myid = Server.HtmlDecode(dv2(0)("CompanyID"))
            Else
                myid = Server.HtmlDecode(dv(0)("ID"))
            End If

            exist = True
        End If

        'conn.Close()

        Dim results As String

        If exist Then
            Qry3 = "SELECT *, con.Email as Expr1 FROM  Companys as comp, Contacts as con WHERE comp.ID = con.CompanyID and comp.ID = '" & myid & "'"
            'conn.Open()
            Dim Sample3 As New DataSet
            Dim Sql3 As New SqlDataAdapter(Qry3, conn)
            Sql3.Fill(Sample3)
            dv.Table = Sample3.Tables(0)


            Dim rating As String

            rating = ""
            results = ""

            Dim i As Integer
            Dim comments As String

            comments = ""


            Dim dates As String

            dates = ""

            Qry4 = "SELECT * FROM  Comments WHERE companyID = '" & myid & "'"
            'conn2.Open()
            Dim Sample4 As New DataSet
            Dim Sql4 As New SqlDataAdapter(Qry4, conn)
            Sql4.Fill(Sample4)
            dv2.Table = Sample4.Tables(0)


            If Sample4.Tables(0).Rows().Count > 0 Then

                For i = 0 To Sample4.Tables(0).Rows().Count - 1
                    If Not isdbnull(dv2(i)("Comment")) Then
						dates = Server.HtmlDecode(dv2(i)("DateCreated"))
						comments = comments & Environment.NewLine & Left(dates, 10) & " " & Server.HtmlDecode(dv2(i)("Comment"))
					End If
                Next i

            End If
            'conn.Close()

            Select Case (Server.HtmlDecode(dv(0)("CustomerRating")))
                Case 0
                    rating = "Processing"
                Case 1
                    rating = "Hot"
                Case 2
                    rating = "Medium"
                Case 3
                    rating = "Cold"
                Case 4
                    rating = "Unknown"
                Case 5
                    rating = "BillingSold"
                Case 6
                    rating = "Dead"
                Case 7
                    rating = "Collections"
                Case 8
                    rating = "Email"
            End Select

            results = "<div id='container'><h2>" & Server.HtmlDecode(dv(0)("FName")) & " " & Server.HtmlDecode(dv(0)("LName")) & "</h2><h3>" & Server.HtmlDecode(dv(0)("Name")) & "</h3><div><div id='address'> <span>" & Server.HtmlDecode(dv(0)("Address")) & "</span><br/><span>" & Server.HtmlDecode(dv(0)("City")) & "</span><Br/><span>" & Server.HtmlDecode(dv(0)("Zip")) & "</span> </div><img src='http://www.bluetonemedia.com/images/BluetoneCalls2/images/phone.jpg' /><span id='number'><a href='tel:" & Server.HtmlDecode(dv(0)("Phone")) & "'>" & Server.HtmlDecode(dv(0)("Phone")) & "</a></span><br/><img src='http://www.bluetonemedia.com/images/BluetoneCalls2/images/link.jpg' /><span class='links'><a href='" & Server.HtmlDecode(dv(0)("Website")) & "' target='_blank'>" & Server.HtmlDecode(dv(0)("Website")) & "</a></span><br/><img src='http://www.bluetonemedia.com/images/BluetoneCalls2/images/email.jpg' /><span class='links'><a href='mailto:" & Server.HtmlDecode(dv(0)("Expr1")) & "'>" & Server.HtmlDecode(dv(0)("Expr1")) & "</a></span><br/></div><div id='notes'> <h4>Recent Notes</h4><p>" & Replace(comments, Chr(10), "<br/>") & "</p></div></div>"
			
			'results = "<b>Customer Rating:</b> " & rating & "<br/><b>Company:</b> " & Server.HtmlDecode(dv(0)("Name")) & "<br/><b>Contact Name:</b> " & Server.HtmlDecode(dv(0)("FName")) & " " & Server.HtmlDecode(dv(0)("LName")) & "<br/><b>Email:</b> " & Server.HtmlDecode(dv(0)("Expr1")) & "<br/><b>Website:</b> <a href='" & Server.HtmlDecode(dv(0)("Website")) & "'>" & Server.HtmlDecode(dv(0)("Website")) & "</a><br/><b class='left'>Next Contact:</b><form action='nextcontact.aspx'><input name='next' class='date' /><input type='submit' value='submit' class='left' /><input name='companyid' value='" & myid & "' type='hidden' /></form><br/><br/><b class='left'>Call Notes:</b> <form action='addcomment.aspx'><input name='companyid' value='" & myid & "' type='hidden' /><input name='comment' /><input name='siteid' value='" & Server.HtmlDecode(dv(0)("Siteid")) & "' type='hidden' /><input type='submit' value='submit' /></form><br/><b>Comment History:</b><br/><textarea rows='10' cols='40'>" & comments & "</textarea><br/><a href='http://www.bluetonemedia.com/admin/company/?id=" & Server.HtmlDecode(dv(0)("ID")) & "'>CRM</a>&nbsp;&nbsp;&nbsp;<a href='http://www.bluetonemedia.com/admin/btmtime/'>Log Call</a>"

            'conn.Close()
        Else
            results = "None"
        End If

		conn.Close()
        Response.Write(results)
        'Response.ContentType = "text/plain"
        Response.Flush()
        Response.End()
    End Sub
End Class
